package net.pacman;

import net.pacman.controller.GameThread;
import net.pacman.util.Constants;
import android.content.Context;
import android.graphics.Canvas;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import net.pacman.model.Labyrinth;
import net.pacman.model.Pacman;

public class GamePanel extends SurfaceView implements SurfaceHolder.Callback,
        Constants {

    private Labyrinth labyrinth;
    private GameThread gameThread;

    public GamePanel(Context context) {
        // TODO Auto-generated constructor stub
        super(context);
        getHolder().addCallback(this);

        labyrinth = new Labyrinth(this);

        gameThread = new GameThread(getHolder(), this);

        setFocusable(true);
    }

    @Override
    public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
        // TODO Auto-generated method stub
    }

    @Override
    public void surfaceCreated(SurfaceHolder arg0) {
        // TODO Auto-generated method stub
        gameThread.setRunning(true);
        gameThread.start();

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder arg0) {
        // TODO Auto-generated method stub
        boolean retry = true;
        while (retry) {
            try {
                gameThread.join();
                retry = false;
            } catch (InterruptedException intExc) {
            }
        }
    }

    @Override
    public void draw(Canvas canvas) {
        // TODO Auto-generated method stub
        super.draw(canvas);
        labyrinth.draw(canvas);
//        Iterator<CharacterView> it = views.iterator();
//        while (it.hasNext()) {
//            CharacterView view = it.next();
//            view.draw(canvas);
//        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
//        Pacman pacman = labyrinth.getPacman();
//        float x = 0, y = 0;
        Log.d("Position Touched","Touched");
        
//        if (event.getAction() == MotionEvent.ACTION_DOWN) {
//            x = event.getX();
//            y = event.getY();
//            Log.d("Position Touched",Float.toString(x) + Float.toString(y));
//        } else {
//        }
        
        return true;
    }

    public void updateStates() {
//        labyrinth.update();
    }
}
